<?php

namespace app\services\car;

use app\dao\car\CarOrderDao;
use app\model\car\CarOrderRefund;
use app\model\car\CarOrderRerent;
use app\model\car\CarTradePayOrder;
use app\model\car\CarUnfreezeOrder;
use app\services\BaseServices;
use think\exception\ValidateException;


class CarOrderStaffServices extends BaseServices
{
    public function __construct(CarOrderDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取结算信息
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBalanceInfo(int $id)
    {
        $order = $this->dao->get($id,['id','car_fee','contact_name','status','type_name','service_cost','is_month','base_service_fee','free_deposit_id','car_service_fee','car_service_name','home_take_price','home_return_price','night_take_fee','night_return_fee','place_fee','city_fee','coupon_price','integral_price','pay_type','pay_time','deposit','illegal_deposit','price']);
        if(!$order){
            throw new ValidateException('未查询到订单信息');
        }
        $order = $order->toArray();
        $pay_type = ['weixin'=>'微信支付','alipay'=>'支付宝支付','yue'=>'余额支付'];
        $order['pay_time'] = $order['pay_time'] ? date('Y-m-d H:i',$order['pay_time']) : '';
        $order['pay_type'] = $pay_type[$order['pay_type']] ?? '';
        $order['deposit_sum'] = bcadd($order['deposit'],$order['illegal_deposit'],2);
        /** @var CarOrderDetailServices $orderDetailServices */
        $orderDetailServices = app()->make(CarOrderDetailServices::class);
        // 当面收费记录
        $order_detail = $orderDetailServices->selectList(['order_id'=>$order['id']],'fee_name,price,pay_type,remark,add_time')->toArray();
        if($order_detail){
            foreach ($order_detail as &$val){
                $val['add_time'] = date('Y-m-d H:i',$val['add_time']);
                $val['pay_type'] = $pay_type[$val['pay_type']] ?? '';
            }
            unset($val);
            $order['order_detail'] = $order_detail;
        }else{
            $order['order_detail'] = false;
        }
        // 押金信息
        if($order['free_deposit_id']){
            /** @var CarFreeDepositOrderServices $freeDepositOrderService */
            $freeDepositOrderService = app()->make(CarFreeDepositOrderServices::class);
            $free_order = $freeDepositOrderService->get($order['free_deposit_id']);
            $free_order = $free_order->toArray();
            $free_order['pay_time'] = $free_order['pay_time'] ? date('Y-m-d H:i',$free_order['pay_time']) : '';
            $free_order['pay_type'] = $pay_type[$free_order['pay_type']]??'';
            $order['free_order'] = $free_order;
        }else{
            $order['free_order'] = false;
        }
        // 续租信息
        /** @var CarOrderRerentServices $renewOrderServices */
        $renewOrderServices = app()->make(CarOrderRerentServices::class);
        $renew_order = $renewOrderServices->selectList(['order_id'=>$order['id'],'is_del'=>0],'id,add_time,rerent_price,pay_time,rerent_status')->toArray();
        if($renew_order){
            $order['renew_order'] = $renew_order;
        }else{
            $order['renew_order'] = false;
        }
        // 押金扣款和退还记录
        $trade_list = CarTradePayOrder::field('total_amount as price,add_time,remark')->where(['car_order_id'=>$order['id']])->select()->toArray();
        $unfreeze_list = CarUnfreezeOrder::field('amount as price,add_time,remark')->where(['car_order_id'=>$order['id']])->select()->toArray();
        $trade_list = array_map(function ($item){
            $item['type'] = 0;
            return $item;
        },$trade_list);
        $unfreeze_list = array_map(function ($item){
            $item['type'] = 1;
            return $item;
        },$unfreeze_list);
        $list = array_merge($trade_list,$unfreeze_list);
        if($list&&count($list) > 1){
            usort($list,function ($a,$b){
                if($a['add_time'] == $b['add_time']){
                    return 0;
                }
                return ($a['add_time'] < $b['add_time']) ? 1 : -1;
            });
        }
        foreach ($list as &$item){
            $item['add_time'] = date('Y-m-d H:i',$item['add_time']);
        }
        $order['deposit_list'] = $list;
        return $order;
    }
}